import Vue from 'vue'
import Router from 'vue-router'
import store from '../store'
import { Message } from "element-ui";

Vue.use(Router)

const router = new Router({
    routes: [
        {
            path: '/login',
            name: 'login',
            component: () => import('@/components/Login'),
            meta: {
                requireAuth: false
            }
        },
        {
            path: '/',
            component: () => import('@/components/Frame'),
            children: [
                {
                    path: '',
                    name: 'Home',
                    component: () => import('@/components/Dashboard'),
                    meta: {
                        requireAuth: true
                    }
                }
            ]
        },
        {
            path: '/hello',
            name: 'hello',
            component: () => import('@/components/HelloWorld'),
            meta: {
                requireAuth: true
            }
        },
        {
            path: '/demo',
            name: 'demo',
            component: () => import('@/components/Demo')
        }
    ]
})

router.beforeEach((to, from, next) => {
    if (to.meta.requireAuth) {
        if (store.state.user == null) {
            Message.error("请先进行登录.");
            next({
                name: 'login',
                query: {
                    redirect: to.path
                }
            })
        } else {
            next()
        }
    } else {
        next()
    }
})
export default router